/*
 * DefaultIOManager.java
 * 
 * last update: 16.01.2010 by Stefan Saru
 * 
 * author:	Alec(panovici@elcom.pub.ro)
 * 
 * Obs:
 */


package engine;

import middle.*;
import java.io.*;

/**
 * The simplest implementation of IO commnication
 * of the simulator. It simply uses System.out & System.in
 *
 */
class DefaultIOManager implements IOManager{

	BufferedReader in;

	/**
	 * Log everything to this file
	 */
	public FileWriter logFile;

	DefaultIOManager(){
		in = new BufferedReader(new InputStreamReader(System.in));
		try{
			logFile = new FileWriter("simulation.log");
		}catch(IOException ex){
			logFile = null;
		}
	}

	public String cin(){
		try{
			String res = in.readLine();
			return res;
		}catch(IOException ex){
			cout("exception: "+ex);
			abort();
		}
		return "cucu";
	}

	synchronized public void cout(String s){
		System.out.print(s);
		System.out.flush();
		try{
			if(logFile != null){
				logFile.write(s + "\n");
				logFile.flush();
			}
		}catch(IOException ex){
			logFile = null; //give up...
		}
	}

	public void abort(){
		cout("abnormal program termination");
		System.exit(-1);
	}

	public void finalize(){
		try{
			if(logFile != null)
				logFile.close();
		}catch(IOException ex){}
	}

}







